package com.jiahua.user.biz.action.customer;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.jiahua.user.biz.model.Customer;
import com.jiahua.user.biz.service.CustomerService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport {

	private static final long serialVersionUID = 8950267292279179686L;

	final static Logger logger = Logger.getLogger(UserLoginAction.class.getName());
	
	CustomerService customerService;

	private String loginid;
	private String password;

	public String getLoginid() {
		return loginid;
	}

	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		logger.info("[1]loginid     : " + loginid);
		logger.info("[2]password    : " + password);

		Customer customer = validateLogin(loginid, password);
		
		if (customer != null) {
			return Action.SUCCESS;
		} else {
			request.setAttribute("loginInfoValidatorMsg", "ログイン情報を確認ください");
			return Action.ERROR;
		}
	}

	public Customer validateLogin(String loginid, String passwd) {
		return customerService.verifyLoginInfo(loginid, passwd);
	}

}
